package selenium;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;

public class daguan {
//达观数据语义分析平台
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		//*********************唯一需要更改的地方********************
		//参考格式  ： D://downloads/flv
		String filepath = "F:\\VDownload";// F:\VDownload\腾讯\影视
		
		int kaiguan = 1 ;//搜索引擎取词开关。1为关闭，4为开启
		int tagMax = 100;//取100代表取走达观所有推荐词，默认100>>>>>>5个最佳,匹配5个标签词，
		//*********************唯一需要更改的地方********************
		String filepath1 = filepath.replace("\\", "\\\\");	
		
		
		File fileExist = new File(filepath + "/ab.txt");
		if (!fileExist.exists()) {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				System.out.println("这是个文件");
				System.out.println("path=" + file.getPath());
				System.out.println("absolutepath=" + file.getAbsolutePath());
				System.out.println("name=" + file.getName());

			} else if (file.isDirectory()) {
				System.out.println("这是个文件夹");
				System.out.println("path=" + file.getPath());
				System.out.println("absolutepath=" + file.getAbsolutePath());
				System.out.println("name=" + file.getName());
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					String fileName = filelist[i];
					String endWith123 = fileName.split(".mp4")[0];
					if ((!fileName.endsWith(".mp4")) || endWith123.endsWith(")")) {
						continue;
					}
					method2(filepath + "/ab" + ".txt", filepath1 + "/" + fileName);
				}
			}
		}
	
		final WebDriver driver = new ChromeDriver();
		Point targetPosition = new Point(300,300);
		driver.manage().window().setPosition(targetPosition );
		driver.manage().window().maximize();
		try {
			driver.get("http://www.datagrand.com/demo/nlp/");
			for (int i = 0; i < 99999; i++) {
				String title = driver.getTitle();
				int state = 0;
				if (!title.equals("达观数据nlpdemo")) {
					state = 1;
					driver.get("http://www.datagrand.com/demo/nlp/");
				}
				driver.manage().deleteAllCookies();
				// 读取视频地址
			File filename1 = new File(filepath + "/ab.txt"); // 要读取以上路径的input。txt文件
			InputStreamReader reader = new InputStreamReader(new FileInputStream(filename1)); // 建立一个输入流对象reader
			BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
			String line1 = "";
			line1 = br.readLine().trim();
			final StringBuffer line2=new StringBuffer();
			line2.append(line1);
			if(line1.isEmpty()){
				break;
			}
			//System.out.println("读入视频文件成功：" + line1);
			
			String lineTile = line1.split("/")[line1.split("/").length-1];
			String textString = lineTile.replaceAll(".mp4", "");
			JavascriptExecutor js= (JavascriptExecutor) driver;
			WebElement editor = driver.findElement(By.tagName("textarea"));
	        js.executeScript(
	                "arguments[0].innerHTML = '"+textString+ "'",
	                editor);
			String ss = driver.findElement(By.className("text-input")).getText();
			final String bss = new String(ss);
			driver.findElement(By.className("apply-button")).click();
			//*[@id="word-cloud"]/div/div/div[1]/div/div
			Thread.sleep(100);
			try {
				WebDriverWait wait = new WebDriverWait(driver, 3);
				wait.until(new ExpectedCondition<WebElement>() {
					@Override
					public WebElement apply(WebDriver d) {
						//*[@id="entity"]/div/div/div[1]/div/div/span
						WebElement ddElement = driver.findElement(By.xpath("//*[@id='entity']/div/div/div[1]/div/div/span"));
						WebElement xaElement = driver.findElement(By.xpath("//*[@id='word-cloud']/div/div/div[1]/div/div"));
						if(ddElement==null||xaElement==null||xaElement.findElements(By.tagName("span"))==null
								||ddElement.getText()==null){
							return null;
						}
						System.out.println("监测点1");
						List<WebElement>  w10 = xaElement.findElements(By.tagName("span"));
						System.out.println("监测点6");
						String xa = "";
						System.out.println("监测点5");
						if(w10.size()>0){
							xa = w10.get(0).getText();
						}else{
							xa = "没有词";
						}
						System.out.println("监测点4");
							if (bss.equals(ddElement.getText())&&(!"雄安新区".equals(xa))) {
								return ddElement;
							}else{
								return null;
							}
					}
				});
			} catch (Exception e) {
				Thread.sleep(500);
				//showSweetAlert 
				System.out.println("监测点2");
				WebElement showSweetAlert = driver.findElement(By.className("showSweetAlert"));
				System.out.println("监测点3");
				if(showSweetAlert!=null){
					WebElement jiancequeren = driver.findElement(By.className("confirm"));
					if(jiancequeren!=null){
						driver.findElement(By.className("confirm")).click();
						Thread.sleep(500);
						continue;
					}
				}
				
			}
			System.out.println("监测点7");
			WebElement jqcloud =driver.findElement(By.className("jqcloud"));
			System.out.println("监测点8");
			Thread.sleep(100);
			List<WebElement> spanTag = jqcloud.findElements(By.tagName("span"));
			System.out.println("监测点9");
			
			StringBuilder myTagStringBuilder = new StringBuilder(); 
			ArrayList<String> myTagMore = new ArrayList<String>();//存储所有的标签（包括百度过来的）
			ArrayList<String> listSpanText = new ArrayList<String>();//最多储存五个标签
			for (WebElement webElement : spanTag) {
				String spanText = webElement.getText();
				listSpanText.add(spanText);
				if(listSpanText.size()<=tagMax){
					myTagStringBuilder.append(spanText);
					myTagStringBuilder.append("/");
				}
				myTagMore.add(spanText);
			}
			if (listSpanText.size()<kaiguan) {
				String sougou = "http://www.sogou.com/web?query="+textString+"&page=";
				String baidu = "https://www.baidu.com/s?wd="+textString+"&pn=";
				Map<String,Integer> map = new HashMap<String,Integer>();
					for (int j = 1; j < 2; j++) {
						String[] arg = new String[2];
						arg[0] = sougou + j;// 搜狗分析
						arg[1] = baidu + 10 * j;// 百度分析
						for (int ii = 0; ii < 2; ii++) {
							if (arg[ii] != null) {
								driver.get(arg[ii]);// 百度和搜狗分析
								List<WebElement> a1 = driver.findElements(By.tagName("em"));
								for (WebElement webElement : a1) {
									String text = webElement.getText().replace(",", "").replace("《", "").replace("》",
											"").replace(":", "").replace("：", "");
									if (map.get(text) == null) {
										map.put(text, 1);
									} else {
										map.put(text, map.get(text) + 1);
									}
								}
							}
						}

					}
				
				 
				// 升序比较器
				Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
					@Override
					public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
						// TODO Auto-generated method stub
						return o2.getValue() - o1.getValue();
					}
				};
				 // map转换成list进行排序
				 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
				 // 排序
				 Collections.sort(list,valueComparator);
				 // 默认情况下，TreeMap对key进行升序排序
				 int ix = 1;
				 for (Map.Entry<String, Integer> entry : list) {
					 myTagMore.add(entry.getKey());
					 if(listSpanText.size()>=5){//多余5个的词不要了
							continue;
						}
					if (ix < 8) {//8个循环内肯定可以把百度过来的词和达观过来的词合并掉
						String entryKey = entry.getKey();
						if(listSpanText.contains(entryKey)){
							continue;
						}else if(entryKey.length()<7&&entryKey.length()>1){//百度过来的超过6个字和小于2个字的词舍弃掉
							myTagStringBuilder.append(entryKey);
							myTagStringBuilder.append("/");
							listSpanText.add(entryKey);
						}
						ix++;
						if(listSpanText.size()>=5){
							continue;
						}
					}
				}
			}
			
			StringBuilder myTagMoreStringBuilder = new StringBuilder(); 
			for (String tagMore : myTagMore) {
				myTagMoreStringBuilder.append(tagMore);
				myTagMoreStringBuilder.append(" ");
				
			}
			String moreTagsubString = myTagMoreStringBuilder.substring(0, myTagMoreStringBuilder.length()-1);
			String abc = myTagStringBuilder.toString();
			String subString = abc.substring(0, abc.length()-1);
			System.out.println("--------------");
			System.out.println("标题整理："+ss);
			System.out.println("标签切分："+subString);
			System.out.println("最多标签："+moreTagsubString);
			System.out.println("--------------");
			method2(filepath + "/a" + ".txt", line1 + "●" + subString+"▲" + moreTagsubString);
			List<String> list = readFile(filepath + "/ab.txt", line1);
			writeFile(filepath + "/ab.txt", list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		 //driver.quit();
	};




	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}

}
